// Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
#include "relu7_tiling_data.h"
#include "exe_graph/runtime/shape_utils.h"
#include "log/op_log.h"
#include "register/op_impl_registry.h"

namespace optiling {
struct Relu7OpCompileInfo {
    int coreNum;
    int ubSize;
};

template <typename T> static void AppendTilingData(gert::TilingData *tilingData, T value)
{
    tilingData->Append<T>(value);
}

ge::graphStatus Relu7TilingPrepare(gert::TilingParseContext *context)
{
    OP_LOGD(context->GetNodeName(), "Relu7TilingPrepare enter.");
    return ge::GRAPH_SUCCESS;
}

ge::graphStatus Relu7Tiling(gert::TilingContext *context)
{
    auto tilingData = context->GetTilingData<optiling::Relu7TilingData>();
    if (tilingData == nullptr) {
        OP_LOGE(context->GetNodeName(), "optiling::Relu7TilingData is null in context");
        return ge::GRAPH_FAILED;
    }

    auto inShape = context->GetInputShape(0);
    if (inShape == nullptr) {
        OP_LOGE(context->GetNodeName(), "InputShape[0] is null in context");
        return ge::GRAPH_FAILED;
    }

    const auto &inShapeValue = gert::EnsureNotScalar(inShape->GetStorageShape());
    auto shapeSize = inShapeValue.GetShapeSize();
    auto inTensor = context->GetInputTensor(0);
    auto dataTypeSize = ge::GetSizeByDataType(inTensor->GetDataType());
    if (dataTypeSize == 0) {
        dataTypeSize = 1;
    }
    int coreNum = 8;
    int default_buf = 256 * 1024;
    tilingData->coreNum = coreNum;
    tilingData->localBufSize = default_buf / dataTypeSize;
    tilingData->loopTimes = (shapeSize + tilingData->localBufSize - 1) / tilingData->localBufSize;
    tilingData->lenEachLoop = tilingData->localBufSize;
    tilingData->lenLastLoop = shapeSize % tilingData->localBufSize;
    tilingData->reserved0 = 0x12345678;
    tilingData->reserved1 = 0x23456789;
    tilingData->reserved2 = 0x34567890;
    tilingData->reserved3 = 0x4567890a;
    tilingData->reserved4 = 0x567890ab;
    tilingData->reserved5 = 0x67890abc;
    tilingData->reserved6 = 0x7890abcd;
    tilingData->reserved7 = 0x890abcde;
    tilingData->reserved8 = 0x90abcdef;
    tilingData->reserved9 = 0x0abcdef0;
    tilingData->reserved10 = 0xabcdef01;

    context->SetBlockDim(tilingData->coreNum);
    OP_LOGE(context->GetNodeName(), "ReluTilingConstruct success");
    return ge::GRAPH_SUCCESS;
}

IMPL_OP(Relu7).Tiling(Relu7Tiling).TilingParse<Relu7OpCompileInfo>(Relu7TilingPrepare);
} // namespace optiling